package com.fh.service.system.dictionaries;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fh.mapper.SystemDictionaryItemMapper;
import com.fh.mapper.SystemDictionaryMapper;
import com.fh.model.system.SystemDictionary;
import com.fh.model.system.SystemDictionaryItem;
import com.fh.query.PageResult;
import com.fh.query.SystemDictionaryItemQueryObject;
import com.fh.query.SystemDictionaryQueryObject;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService{
	@Autowired
	private SystemDictionaryItemMapper itemMapper;
	@Autowired
	private SystemDictionaryMapper mapper;
	/**
	 * 字典明细的高级查询
	 * 
	 */
	public PageResult queryItem(SystemDictionaryItemQueryObject qo) {
		int count = itemMapper.queryByCount(qo);
		if (count > 0) {
			List<SystemDictionaryItem> list = itemMapper.query(qo);
			return new PageResult(list, count, qo.getCurrentPage(), qo.getPageSize());
		}
		return PageResult.empty(qo.getPageSize());
	}

	/**
	 * 字典目录高级查询
	 * 
	 */
	public PageResult queryDis(SystemDictionaryQueryObject qo) {
		int count = mapper.queryByCount(qo);
		System.out.println(count);
		if (count > 0) {
			List<SystemDictionary> list = mapper.query(qo);
			return new PageResult(list, count, qo.getCurrentPage(), qo.getPageSize());
		}
		return PageResult.empty(qo.getPageSize());
	}

	/**
	 * 查询所有的数据字典目录
	 * 
	 */
	public List<SystemDictionary> selectAll() {
		return mapper.selectAll();
	}


	/**
	 * 新增或更新一条字典目录
	 * 
	 */
	public void saveOrUpdate(SystemDictionary dic) {
		if (dic.getId() != null) {
			mapper.updateByPrimaryKey(dic);
		}else{
			mapper.insert(dic);
		}
	}
	
	/**
	 * 新增或更新一条字典明细
	 * 
	 */
	public void saveOrUpdateItem(SystemDictionaryItem item) {
		if (item.getId() != null) {
			itemMapper.updateByPrimaryKey(item);
		}else{
			itemMapper.insert(item);
		}
	}

	/**
	 * 通过sn查询所有的字典明细
	 * 
	 */
	public List<SystemDictionaryItem> selectItemBySn(String sn) {
		return itemMapper.selectItemBySn(sn);
	}

    @Override
    @Transactional
    public int deleteByisDel1(long id) {
        int a = itemMapper.deleteByisDel1(id);
        int b =  itemMapper.deleteByisDel2(id);
       return  a+b;
    }

    @Override
    @Transactional
    public int deleteByisDel3(long id) {
        return  itemMapper.deleteByisDel3(id);
    }

}
